/* eslint-disable default-case */
/* eslint-disable consistent-return */
import { message } from 'antd';
import {
  getProductList,
  getEmpDetailList,
  submitModalData,
  addSubmitData,
  downloadFile,
  deleteRecord,
  speedProgress,
  custcontList,
  submitFileData,
  addEmpData,
  getSuppDataList,
  supplierAddDataList,
  addGetDataList,
  getSupplierDataList,
  addModalSubmitData,
  getContDataList,
  deleteListInfo,
  // submitData,
  getContInfo,
  fetchCustUpdate,
  updateSubmitdate,
  getUpdateDetail,
  submitEmpUpdate,
  suppUpdateData,
  suppSuppData,
  addSuppModalData,
  getEmpNameList,
  getEsEmpNameList,
  uploadFile,
  uploadFileSupp,
} from '@/services/oncechargecs';
import * as service from '@/services/socket';

export default {
  state: {
    product: [], // 产品下拉
    empDataList: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    }, // 向员工收费 - 员工明细列表

    suppDataList: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    }, // 向供应商收费 - 员工明细列表
    empNameList: [],
    batchQuery: {},
    currentStep: 0,
    showProgress: false,
    progressPercent: 0,
    resultInfo: {
      // 结果数据必传
      batchName: '', // 任务名称
      taskCount: 0, // 导入数据
      successCount: 0, // 成功数据
      errorCount: 0, // 失败数据
      batchNo: '', // 批次号
      errorUrl: '/csApi/oncefeeFile/dowoloadEmpDet', // 下载错误信息地址
      errorParams: null, // 下载错误数据参数
    },
  },
  effects: {
    // 产品下拉
    *getProductList({ payload }, { call, put }) {
      yield put({
        type: 'updateModelData',
        payload: { product: [] },
      });
      const response = yield call(getProductList, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            product: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取产品列表失败');
      }
    },

    // 获取雇员明细
    *geteEmpList({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;

      yield put({
        type: 'getEmpDataList',
        payload: {
          empDataList: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(getEmpDetailList, payload);
      if (response.success) {
        yield put({
          type: 'getEmpDataList',
          payload: {
            empDataList: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '获取列表失败');
      }
    },

    *submitModalInfo({ payload, callback }, { call }) {
      const response = yield call(submitModalData, payload.submitData);
      callback(response);
    },

    // 新增提交
    submitDataList: [
      function*({ payload }, { call }) {
        const response = yield call(addSubmitData, payload);
        return response;
      },
      { type: 'throttle', ms: 10000 },
    ],
    // 下载
    *download({ payload }, { call }) {
      const response = yield call(downloadFile, payload);
      return response;
    },

    // 删除
    *deleteData({ payload }, { call }) {
      const response = yield call(deleteRecord, payload);
      return response;
    },

    // 进度
    *speedOfProgress({ payload }, { call }) {
      const response = yield call(speedProgress, payload);
      return response;
    },

    // 获取商务合同
    *getCustcontList({ payload }, { call }) {
      const response = yield call(custcontList, payload);
      return response;
    },

    // 提交文件
    *submitFile({ payload }, { call }) {
      const response = yield call(submitFileData, payload);
      return response;
    },

    // 新增
    submitAddEmpData: [
      function*({ payload }, { call }) {
        const response = yield call(addEmpData, payload);
        return response;
      },
      { type: 'throttle', ms: 10000 },
    ],
    // 获取供应商下拉
    *getSuppList({ payload }, { call }) {
      const response = yield call(getSuppDataList, payload);
      return response;
    },

    // 提交向供应商收费
    supplierAddData: [
      function*({ payload }, { call }) {
        const response = yield call(supplierAddDataList, payload);
        return response;
      },
      { type: 'throttle', ms: 10000 },
    ],

    // 获取商务合同
    *getSupplierList({ payload }, { call }) {
      const response = yield call(getSupplierDataList, payload);
      return response;
    },

    // 向供应商收费 - 获取商务合同
    *getContList({ payload }, { call }) {
      const response = yield call(getContDataList, payload);
      return response;
    },

    // 删除数据
    *deleteRecordData({ payload }, { call }) {
      const response = yield call(deleteListInfo, payload);
      return response;
    },

    // 获取雇员明细
    *addGetList({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;

      yield put({
        type: 'getAddSuppDataList',
        payload: {
          suppDataList: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(addGetDataList, payload);
      if (response.success) {
        yield put({
          type: 'getAddSuppDataList',
          payload: {
            suppDataList: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '获取列表失败');
      }
    },

    // 模态框新增提交数据
    *modalSubmitData({ payload }, { call }) {
      const response = yield call(addModalSubmitData, payload);
      return response;
    },

    // 获取商务合同
    *getContListData({ payload }, { call }) {
      const response = yield call(getContInfo, payload);
      return response;
    },

    // 向客户收费修改查看详情
    *fetchCustUpdateData({ payload }, { call }) {
      const response = yield call(fetchCustUpdate, payload);
      return response;
    },

    // 向客户收费 - 修改提交
    *updateSubmit({ payload }, { call }) {
      const response = yield call(updateSubmitdate, payload);
      return response;
    },

    // 向雇员收费 - 修改查看详情
    *updateDetail({ payload }, { call }) {
      const response = yield call(getUpdateDetail, payload);
      return response;
    },

    // 向雇员收费 - 修改提交
    *submitEmpData({ payload }, { call }) {
      const response = yield call(submitEmpUpdate, payload);
      return response;
    },

    // 向供应商收费 - 修改查看详情
    *fetchSuppUpdateData({ payload }, { call }) {
      const response = yield call(suppUpdateData, payload);
      return response;
    },

    // 向供应商收费 - 修改提交
    *supplierSuppData({ payload }, { call }) {
      const response = yield call(suppSuppData, payload);
      return response;
    },

    // 向供应商收费 - 弹框提交
    *addSuppData({ payload }, { call }) {
      const response = yield call(addSuppModalData, payload);
      return response;
    },
    // 单个调整：输入姓名,证件号模糊查询
    *fetchEmpNameList({ payload, isEs = false }, { call, put }) {
      const response = yield call(isEs ? getEsEmpNameList : getEmpNameList, payload);
      yield put({
        type: 'updateModelData',
        payload: { empNameList: [] },
      });
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: { empNameList: response.data },
        });
        return response.data;
      }
    },
    // 获取下载模板fileId fileName
    *getDownloadInfo({ payload }, { call, put }) {
      const response = yield call(downloadFile, payload);
      yield put({
        type: 'updateQuery',
        payload,
      });
      if (response.success) {
        const fileInfo = response.data.split('|');
        yield put({
          type: 'publicdata/setFileDownload',
          payload: { fileId: fileInfo[0], fileName: fileInfo[1] },
        });
      }
    },
    // 上传文件:批量进度
    *uploadFileWithProgress({ payload, custOrSupp }, { call, put }) {
      const response = yield call(custOrSupp === 1 ? uploadFile : uploadFileSupp, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            batchId: response.data && response.data.batchId,
            progressPercent: 0,
          },
        });
        if (custOrSupp === 1) {
          yield put({
            type: 'updateModelData',
            payload: {
              onceFeeCustDraftId: response.data && response.data.onceFeeCustDraftId,
            },
          });
        } else {
          yield put({
            type: 'updateModelData',
            payload: {
              onceFeeDrafId: response.data && response.data.onceFeeDrafId,
            },
          });
        }
        if (response.data && response.data.successProp !== 100) {
          yield call(service.socketChannel, {
            type: 'sub',
            data: { to: response.data && (response.data.batchId || response.data), type: 'task' },
          });
        } else {
          yield put({
            type: 'updateBatchTask',
            payload: response.data,
          });
        }
      } else {
        // 上传失败
        message.warning(`${response.message || '上传失败'}`);
      }
      return response;
    },
    // 匹配唯一batchId
    *matchBatchId({ payload }, { put, select }) {
      const { batchId } = yield select(state => state.oncechargecs);
      if (batchId === payload.batchId) {
        yield put({
          type: 'updateBatchTask',
          payload,
        });
      }
    },
  },

  reducers: {
    // 更新数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 向雇员收费 - 员工明细
    getEmpDataList(
      state,
      {
        payload: { empDataList, currentPage, pageSize },
      }
    ) {
      const list =
        empDataList && empDataList.pageRecords && Array.isArray(empDataList.pageRecords)
          ? empDataList.pageRecords
          : [];
      return {
        ...state,
        empDataList: {
          list,
          pagination: { total: empDataList.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    // 向供应商收费 - 员工明细
    getAddSuppDataList(
      state,
      {
        payload: { suppDataList, currentPage, pageSize },
      }
    ) {
      const list =
        suppDataList && suppDataList.pageRecords && Array.isArray(suppDataList.pageRecords)
          ? suppDataList.pageRecords
          : [];
      return {
        ...state,
        suppDataList: {
          list,
          pagination: { total: suppDataList.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
    // 更新批量任务数据
    updateBatchTask(state, { payload }) {
      let { showProgress, resultInfo, currentStep } = state;
      const { cacheKey } = state;
      showProgress = true;
      if (payload.successProp === 100) {
        resultInfo.errorParams = {
          batchId: payload.batchId,
          cacheKey,
        };
        resultInfo = { ...resultInfo, ...payload };
        currentStep += 1;
        showProgress = false;
      }
      return {
        ...state,
        showProgress,
        progressPercent: payload.successProp,
        resultInfo,
        currentStep,
      };
    },
  },
  subscriptions: {
    socket({ dispatch }) {
      return service.listen(action => {
        switch (action.type) {
          case 'task':
            dispatch({
              type: 'matchBatchId',
              payload: action.data,
            });
            // 全局保存
            dispatch({
              type: 'publicdata/updateBatchTaskData',
              payload: action.data,
            });
            break;
        }
      });
    },
  },
};
